using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;

namespace Sunny.UI;

public static class AttributeEx
{
	public static string Description(this Enum value)
	{
		DescriptionAttribute customAttribute = value.GetType().GetField(value.ToString()).GetCustomAttribute<DescriptionAttribute>();
		if (customAttribute == null)
		{
			return value.ToString();
		}
		return customAttribute.Description;
	}

	public static string Description(this Type type, string propertyName)
	{
		DescriptionAttribute customAttribute = type.GetProperty(propertyName).GetCustomAttribute<DescriptionAttribute>();
		if (customAttribute == null)
		{
			return propertyName;
		}
		return customAttribute.Description;
	}

	public static string Description(this Type type)
	{
		DescriptionAttribute customAttribute = type.GetCustomAttribute<DescriptionAttribute>();
		if (customAttribute == null)
		{
			return type.Name;
		}
		return customAttribute.Description;
	}

	public static string ClassDescription<T>(this T value) where T : class, new()
	{
		return typeof(T).Description();
	}

	public static string ClassPropertyDescription<T>(this T type, string propertyName) where T : class, new()
	{
		return typeof(T).Description(propertyName);
	}

	public static string StructDescription<T>(this T value) where T : struct
	{
		return typeof(T).Description();
	}

	public static string StructPropertyDescription<T>(this T type, string propertyName) where T : struct
	{
		return typeof(T).Description(propertyName);
	}

	public static bool As(this Type type, Type baseType)
	{
		if (type == null)
		{
			return false;
		}
		if (baseType.IsGenericTypeDefinition && type.IsGenericType && !type.IsGenericTypeDefinition)
		{
			type = type.GetGenericTypeDefinition();
		}
		if (type == baseType)
		{
			return true;
		}
		if (baseType.IsAssignableFrom(type))
		{
			return true;
		}
		bool flag = false;
		if (baseType.IsInterface)
		{
			if (type.GetInterface(baseType.FullName) != null)
			{
				flag = true;
			}
			else if (type.GetInterfaces().Any((Type e) => (!e.IsGenericType || !baseType.IsGenericTypeDefinition) ? (e == baseType) : (e.GetGenericTypeDefinition() == baseType)))
			{
				flag = true;
			}
		}
		if (!flag && type.Assembly.ReflectionOnly)
		{
			while (!flag && type != typeof(object))
			{
				if (!(type == null))
				{
					if (type.FullName == baseType.FullName && type.AssemblyQualifiedName == baseType.AssemblyQualifiedName)
					{
						flag = true;
					}
					type = type.BaseType;
				}
			}
		}
		return flag;
	}

	public static bool IsList(this Type type)
	{
		if (type != null && type.IsGenericType)
		{
			return type.As(typeof(IList<>));
		}
		return false;
	}

	public static bool IsDictionary(this Type type)
	{
		if (type != null && type.IsGenericType)
		{
			return type.As(typeof(IDictionary<, >));
		}
		return false;
	}

	public static T GetCustomAttribute<T>(this PropertyInfo info)
	{
		return (T)info.GetCustomAttributes(typeof(T), inherit: false).FirstOrDefault();
	}

	public static T GetCustomAttribute<T>(this FieldInfo info)
	{
		return (T)info.GetCustomAttributes(typeof(T), inherit: false).FirstOrDefault();
	}

	public static T GetCustomAttribute<T>(this Type type)
	{
		return (T)type.GetCustomAttributes(typeof(T), inherit: false).FirstOrDefault();
	}

	public static string GetName(this PropertyInfo pi)
	{
		string result = pi.Name;
		XmlElementAttribute customAttribute = pi.GetCustomAttribute<XmlElementAttribute>();
		if (customAttribute != null && !customAttribute.ElementName.IsNullOrEmpty())
		{
			result = customAttribute.ElementName;
		}
		return result;
	}

	public static string DisplayName(this PropertyInfo member)
	{
		DisplayNameAttribute customAttribute = member.GetCustomAttribute<DisplayNameAttribute>();
		if (customAttribute != null && !customAttribute.DisplayName.IsNullOrWhiteSpace())
		{
			return customAttribute.DisplayName;
		}
		return "";
	}

	public static string Description(this PropertyInfo member)
	{
		DescriptionAttribute customAttribute = member.GetCustomAttribute<DescriptionAttribute>();
		if (customAttribute != null && !customAttribute.Description.IsNullOrWhiteSpace())
		{
			return customAttribute.Description;
		}
		return "";
	}

	public static List<PropertyInfo> GetNeedProperties(this Type type)
	{
		PropertyInfo[] properties = type.GetProperties();
		List<PropertyInfo> list = new List<PropertyInfo>();
		PropertyInfo[] array = properties;
		foreach (PropertyInfo propertyInfo in array)
		{
			if (propertyInfo.GetCustomAttribute<XmlIgnoreAttribute>() == null && propertyInfo.GetCustomAttribute<ConfigIgnoreAttribute>() == null && propertyInfo.GetCustomAttribute<MapperIgnoreAttribute>() == null && propertyInfo.GetCustomAttribute<IgnoreAttribute>() == null)
			{
				list.Add(propertyInfo);
			}
		}
		return list;
	}
}
